package com.kma.ncpractice2013.servlets;

import com.kma.ncpractice2013.facade.MailFacade;
import com.kma.ncpractice2013.facade.MailFacadeImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA.
 * User: HP
 * Date: 08.10.13
 * Time: 18:04
 * To change this template use File | Settings | File Templates.
 */

public class MailServlet extends HttpServlet
{
    MailFacade mail=new MailFacadeImpl();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        String email = request.getParameter("email");
        String subject = request.getParameter("subject");
        String letter = request.getParameter("letter");
        try
        {
            mail.sendLetter(email, subject, letter);
            RequestDispatcher dispatcher = request.getRequestDispatcher("confirmation.jsp");
            if (dispatcher != null)
            {
                dispatcher.forward(request, response);
            }
        }
        catch (Exception e)
        {
            out.print(e.getMessage());
        }


    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);


    }

}
